package subject_set.offer100;

import java.util.LinkedList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/10/15 15:52
 **/
public class Offer32_Ⅲ {
    class Solution {
        List<List<Integer>> res = new LinkedList<>();
        public List<List<Integer>> levelOrder(TreeNode root) {
            if(root == null) return res;
            preOrder(root, 0);
            return res;
        }
        private void preOrder(TreeNode root, int k){
            if(root == null) return;
            if(res.size() <= k) res.add(new LinkedList<>());
            if(k % 2 == 0){
                res.get(k).add(root.val);
            }else res.get(k).add(0, root.val);
            preOrder(root.left, k + 1);
            preOrder(root.right, k + 1);
        }
    }
}